function [data,time,titles] = read_flowjo_table_time(flow_path)

%read frequency in gates file from flowjo tables
%In FlowJo Columns: [Specimen,Q1,Q2,Q3,Q4,Geo-FSC,Geo-SSC,'']
%Out:
%   data = [row,col,rep,gate]
%   titles = 1 per replicate
%   time = 1 per replicate, in minutes
%USAGE: [data,time,titles] = read_flowjo_table_time('../All_Lim_FACS_tvs/121012_96all.tab')
fin = fopen(flow_path,'r');
line = 1;
rep = 0;
%assumes 96-well format, max 20 replicates, 6 params
data = NaN(8,12,20,6); 
titles = '';
well_exp = '_([ABCDEFGH])(\d+)_';
time_pat = '_t(\d*)h?(\d*)m?'; %enter time as t3h30m, e.g.

while line>-1
    line = fgetl(fin);
%Q1-4: red, hetero, green, dead
    if any(line=='>')
        rep = rep+1;
        timestr = regexp(line,time_pat,'tokens');
        time(rep) = str2num(timestr{1}{1})*60;
        if ~isempty(timestr{1}{2})
           time(rep) = time(rep) + str2num(timestr{1}{2});
        end
        titles{rep} = line;
        titles{rep}(find(titles{rep}=='_'))=' ';
        fgetl(fin);
    end
    if strfind(line,'Specimen')
        tokens = regexp(line,well_exp,'tokens');
        row = uint8(find('ABCDEFGH'==tokens{1}{1}));
        col = uint8(str2num(tokens{1}{2}));
        freq = regexp(line,'\t','split');
        %Handle n/a calculations
        for i = 1:numel(freq)
            if strcmp(freq{i},'n/a')
                freq{i} = 'NaN';
            end
        end 
        data(row,col,rep,:) = cellfun(@str2num,freq(2:end-1));
    end
end